import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import { getInfo } from '@/api/login'

export default new Vuex.Store({
	state: {
		token: localStorage.getItem('token'),
		info: null,

		showLoginToast: false // 显示登录弹窗
	},
	getters: {
		token: (state) => state.token,
		info: (state) => state.info,
		showLoginToast: (state) => state.showLoginToast
	},
	mutations: {
		SET_TOKEN(state, token) {
			state.token = token
			localStorage.setItem('token', token)
		},
		SET_INFO(state, info) {
			state.info = info
		},
		SET_SHOW_LOGIN_TOAST(state, visible) {
			state.showLoginToast = visible
		}
	},
	actions: {
		Login({ commit }, { token, info }) {
			commit('SET_TOKEN', token)
			commit('SET_INFO', info)
		},
		async getInfo({ commit }) {
			const { data } = await getInfo()
			commit('SET_INFO', data)
		}
	},
	modules: {}
})
